package tuding.android.bigplanettracks.maps.tuding;

import java.util.ArrayList;

import android.graphics.Point;

import tuding.android.bigplanettracks.maps.RawTile;
import tuding.android.bigplanettracks.maps.tuding.GMOffsetBase;

public class GMOffsetList extends ArrayList<GMOffsetBase> {

	/**
	 * 
	 */
	public static ArrayList<GMOffsetBase> GMOffsets = new ArrayList<GMOffsetBase>();
	private static final long serialVersionUID = 8394150218887830912L;
	
	public static int indexOfTile(RawTile tile) {
		//Log.w("OFFSET", "tile "+tile.x+" "+tile.y+" "+tile.z);
		for(int i=0; i<GMOffsets.size(); i++) {
			int shiftval = 0;
			if(7>=tile.z) {
				//Log.i("OFFSET", "---- index: "+(tile.x >> (7-tile.z))+" "+(tile.y >> (7-tile.z))+" exp: "+GMOffsets.get(i).index_X+" "+GMOffsets.get(i).index_Y);
				shiftval = 7-tile.z;
				if(GMOffsets.get(i).index_X == (tile.x >> shiftval) && (GMOffsets.get(i).index_Y == (tile.y >> shiftval))) {
					//Log.i("OFFSET","HIT !!!:"+(tile.x >> shiftval)+" "+(tile.y >> shiftval)+" index: "+i);
					return i;
				}
			} else {
				shiftval = tile.z-7;
				//Log.i("OFFSET", "---- index2: "+(tile.x << shiftval)+" "+(tile.y << shiftval)+" exp: "+GMOffsets.get(i).index_X+" "+GMOffsets.get(i).index_Y);
				if(GMOffsets.get(i).index_X == (tile.x << shiftval) && (GMOffsets.get(i).index_Y == (tile.y << shiftval))) {
					//Log.i("OFFSET","HIT !!!"+(tile.x << shiftval)+" "+(tile.y << shiftval)+" index: "+i);
					return i;
				}
			}
			
			
		}
		return -1;
	}
	

	
	public static Point getOffset(int i) {
		Point rc = new Point(0, 0);
		try {
			rc.x = GMOffsets.get(i).offset_X;
			rc.y = GMOffsets.get(i).offset_Y;
			//Log.i("OFFSET", "getOffset: "+rc.x+" "+rc.y+" index: "+i);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rc;
	}
	
}